/*
package com.wtp.wallpaper.rabbitmq.producer;

import com.wtp.wallpaper.rabbitmq.data.DataChangeEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.UUID;

@Component
@Slf4j
public class RabbitMQProducer {
    
    private final RabbitTemplate rabbitTemplate;

    private final RetryTemplate retryTemplate;
    
    public RabbitMQProducer(RabbitTemplate rabbitTemplate, RetryTemplate retryTemplate) {
        this.rabbitTemplate = rabbitTemplate;
        this.retryTemplate = retryTemplate;
        // 初始化确认回调
        initConfirmCallback();
        initReturnCallback();
    }

    // 初始化消息到达交换机的确认回调
    private void initConfirmCallback() {
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (correlationData == null) return;
            
            if (ack) {
                log.info("消息成功到达交换机，ID: {}", correlationData.getId());
            } else {
                log.error("消息投递交换机失败，ID: {}，原因: {}", correlationData.getId(), cause);
                // 持久化失败消息（可存数据库/Redis）

                Message failedMessage = Objects.requireNonNull(correlationData.getReturned()).getMessage();
                saveFailedMessageToDB(failedMessage);
            }
        });
    }

    // 初始化消息无法路由到队列的回调
    private void initReturnCallback() {
        rabbitTemplate.setReturnsCallback(returned -> {
            Message message = returned.getMessage();
            log.error("消息无法路由到队列，消息ID: {}，应答码: {}，原因: {}",
                    message.getMessageProperties().getCorrelationId(),
                    returned.getReplyCode(),
                    returned.getReplyText());
            // 处理路由失败消息
            saveFailedMessageToDB(message);
        });
    }

    @Retryable(value = {AmqpException.class}, maxAttempts = 3, backoff = @Backoff(delay = 1000))
    public void sendPictureMessage(DataChangeEvent event) {
        retryTemplate.execute(context -> {
            try {
                // 生成唯一消息ID
                String messageId = UUID.randomUUID().toString();
                
                // 构建消息持久化属性
                MessagePostProcessor postProcessor = message -> {
                    message.getMessageProperties()
                        .setDeliveryMode(MessageDeliveryMode.PERSISTENT);

                    return message;
                };

                // 发送消息（携带CorrelationData）
                CorrelationData correlationData = new CorrelationData(messageId);
                correlationData.setReturnedMessage(
                    rabbitTemplate.getMessageConverter().toMessage(event, new MessageProperties())
                );
                
                rabbitTemplate.convertAndSend(
                    "sync.exchange", 
                    "es.routing.key", 
                    event, 
                    postProcessor, 
                    correlationData
                );
                
                return null;
            } catch (AmqpException e) {
                log.error("消息发送异常，事件: {}", event, e);
                throw e;  // 触发重试
            }
        });
    }

    // 持久化失败消息到数据库（示例）
    private void saveFailedMessageToDB(Message message) {


    }
}*/
